///////////////////////////////////////////////////////////////////////////////
// Project: Hoyo Client Game
//
// hy_formation_logic.cpp
// Created: 2013-7-30
// Author: Like Cheng
///////////////////////////////////////////////////////////////////////////////

#include "hy_formation_logic.h"

#include "hy_net/hy_network_default.h"
#include "hy_net/hy_network_message.h"

#include "hy_common_system/hy_common_func.h"

#include "hy_battle_system/hy_battle_unit.h"
#include "hy_battle_system/hy_battle_data.h"

#include "hy_team_system/hy_team_data.h"
#include "hy_team_system/hy_team_logic.h"

#include "hy_player_system/hy_player_data.h"
#include "hy_player_system/hy_player_logic.h"

#include "hy_battle_system/hy_formation_data.h"

USING_NS_HY_NETWORK;
hy_formation_logic* hy_formation_logic_instance = NULL;

hy_formation_logic* hy_formation_logic::instance()
{
	if( hy_formation_logic_instance == NULL )
		hy_formation_logic_instance = new hy_formation_logic;
	return hy_formation_logic_instance;
}
void hy_formation_logic::purge_instance()
{
	delete hy_formation_logic_instance;
}
hy_formation_logic::hy_formation_logic()
{
	init();
}
hy_formation_logic::~hy_formation_logic()
{

}
void hy_formation_logic::init()
{

}

void hy_formation_logic::proc_formation_change( void* mg )
{
	hy_network_message* msg = (hy_network_message*)mg;

	int player_id = msg->get_player_id();
}

bool hy_formation_logic::send_battle_formation_by_player( int player_id )
{
	hy_network_message msg;

	/*
	int_vec team_player;
	hy_team_data* td = hy_team_logic::instance()->get_team_data_by_player_id( player_id );
	if( td )
	{
		team_player = td->get_team_list();
	}
	else
	{
		team_player.push_back( player_id );
	}
	
	send_battle_formation( player_id, team_player );
	*/
	return true;
}

bool hy_formation_logic::send_battle_formation( int player_id, int_vec& iv )
{
	hy_player_data* pd = hy_player_logic::instance()->get_player_data_by_player_id( player_id );
	if( !pd )return false;

	hy_network_message msg;
	msg.set_protocol_id( SID_BATTLE_FORMATION );

	hy_formation_data* fd = pd->get_formation_data();
	if( !fd )return false;

	Json::Value jv( Json::arrayValue );
	hy_formation_unit_data_map::iterator it;
	for( it = fd->m_formation_unit_data_map.begin(); it != fd->m_formation_unit_data_map.end(); it++ )
	{
		hy_formation_unit_data* fud = it->second;
		Json::Value cv;
		cv["pos"] = fud->m_pos;
		cv["type"] = fud->m_type;
		cv["id"] = fud->m_id;
		hy_common_data* cd = fud->m_ptr;
		cv["head_icon"] = 1;
		bool is_flag = false;
		if( fud->m_type == battle_unit_type_player )
		{
			if( fud->m_id == player_id )
				is_flag = true;
		}
		cv["is_leader"] = is_flag;
	}	
	send_player_list_msg( iv, &msg );

	return true;
}

int hy_formation_logic::get_formation_free_pos( hy_formation_data* fd )
{
	if( fd->m_formation_unit_data_map.size() == 0 )return 1;
	hy_formation_unit_data_map::iterator it;
	for( it = fd->m_formation_unit_data_map.begin(); it != fd->m_formation_unit_data_map.end(); it++ )
	{
		int pos = it->first;
		bool is_free = true;
		for( int i = 1; i <= 6; i++ )
		{
			if( pos == i )
				is_free = false;
		}
		if( !is_free )
			return pos;
	}
	return 0;
}

bool hy_formation_logic::add_formation_player_uint_by_id( int player_id, int add_player_id, int pos )
{
	hy_player_data* player_data = hy_player_logic::instance()->get_player_data_by_player_id( player_id );
	if( !player_data )
	{
		return false;
	}
	return add_formation_player_unit( player_data->get_formation_data(), add_player_id, pos );
}

bool hy_formation_logic::add_formation_player_unit( hy_formation_data* fd , int player_id, int pos_ )
{
	hy_player_data* pd = hy_player_logic::instance()->get_player_data_by_player_id( player_id );
	if( !pd )return false;
	int pos = 0;
	if( pos_ > 0 && pos_ <= 6 )
	{
		pos = pos_;
	}
	else
	{
		pos = get_formation_free_pos( fd );
	}	
	if( !pos )return false;

	hy_formation_unit_data* ud = new hy_formation_unit_data;
	ud->m_type = battle_unit_type_player;
	ud->m_id = pd->get_player_id();
	ud->m_pos = pos;
	ud->m_ptr = pd;

	fd->add_formation_unit_data( ud );

	return true;
}